//
//  MeFileViewController.m
//  RainbowFM
//
//  Created by jv on 14-12-7.
//  Copyright (c) 2014年 RB. All rights reserved.
//

#import "MeFileViewController.h"
#import "MeFileHeadCell.h"
#import "MeFileNormalCell.h"
#import <UIActionSheet+Blocks.h>
#import "UserModel.h"
#import <UIImageView+AFNetworking.h>
#import <PECropViewController.h>

@interface MeFileViewController ()<UIActionSheetDelegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate,PECropViewControllerDelegate>

@end

@implementation MeFileViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
//    [self _initVar];
//    [self _initView];
}


-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self _initVar];
    
}

#pragma mark - init

- (void)_initVar
{
    [[UserModel sharedModel] fetchUserProfile:^(BOOL success) {
        _selIndex = -1;
        
        _titleData = @[
                       @"头像",
                       @"昵称",
                       @"性别",
                       @"生日",
                       @"所在地",
                       @"个性签名",
                       @"修改密码"
                       ];
        NSString *sex = nil;
        switch ([[UserModel sharedModel].userInfo.sex intValue]) {
            case 0:
                sex = @"男";
                break;
            case 1:
                sex = @"女";
                break;
            default:
                sex = @"未设置";
                break;
        }
        _infoData = [@[
                       @"",
                       [UserModel sharedModel].userInfo.nickName,
                       sex,
                       [UserModel sharedModel].userInfo.birthday,
                       [UserModel sharedModel].userInfo.address,
                       [UserModel sharedModel].userInfo.sign,
                       @"点击修改"
                       ] mutableCopy];
        [self _initView];
    }];
    
}

- (void)_initView
{

    [self.tableView reloadData];
    
}

#pragma mark - table datasource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [_titleData count];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    UITableViewCell *fcell;
    NSInteger row = indexPath.row;
    if (row == 0) {
        MeFileHeadCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MeFileHeadCell"];
        fcell = cell;
        cell.isFouced = _selIndex == row;
        cell.nameLabel.text = _titleData[row];
        [cell.headView sd_setImageWithURL:[NSURL URLWithString:[UserModel sharedModel].userInfo.headPic]];
    }else{
        MeFileNormalCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MeFileNormalCell"];
        fcell = cell;
        cell.isFouced = _selIndex == row;
        cell.nameLabel.text = _titleData[row];
        cell.infoLabel.text = _infoData[row];
    }
    
    
    return fcell;
    
}



#pragma mark - table delegate

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 82.f;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    _selIndex = indexPath.row;
    
    [self.tableView reloadData];
    
    UIViewController *ctrl;
    switch (indexPath.row) {
        case 0:
        {
            UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"照片选取"
                                                                     delegate:self
                                                            cancelButtonTitle:@"取消"
                                                       destructiveButtonTitle:nil
                                                            otherButtonTitles:@"相机",@"相册选取", nil];
            [actionSheet showInView:self.view];
//            CropImageViewController *ctrl = [CropImageViewController new];
//            ctrl.delegate = self;
//            [self.navigationController pushViewController:ctrl animated:YES];
        }
            break;
        case 1:
        {
            _nickCtrl = [SingleLineInfoViewController new];
            _nickCtrl.delegate = self;
            _nickCtrl.placeHolderString = @"好的昵称可以帮你找到朋友";
            _nickCtrl.singleType = 0;
//            _nickCtrl.textField.text = _infoData[indexPath.row];
            _nickCtrl.title = @"昵称";
            [self.navigationController pushViewController:_nickCtrl animated:YES];
        }
            break;
        case 2:
        {
//            __weak typeof (self) _self = self;
            NSArray *data = @[@"男",@"女"];
            [UIActionSheet showInView:self.view withTitle:@"性别" cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:data tapBlock:^(UIActionSheet *actionSheet, NSInteger buttonIndex) {
                
               [[UserModel sharedModel] editUserProfileSex:[NSString stringWithFormat:@"%d",buttonIndex]
                                                    result:^(BOOL success) {
//                                                        _infoData[2] = [NSString stringWithFormat:@"%d",buttonIndex];
                                                        [self _initVar];
//                                                        [_self.tableView reloadData];
                                                    }];
                
            }];
            
        }
            break;
        case 3:
        {
            CustomDatePickerViewController *ctrl = [CustomDatePickerViewController new];
            ctrl.delegate = self;
            [self.navigationController pushViewController:ctrl animated:YES];
        }
            break;
        case 4:
        {
            AreaTableViewController *ctrl = [AreaTableViewController new];
            ctrl.areaType = 0;
            ctrl.delegate = self;
            [self.navigationController pushViewController:ctrl animated:YES];

        }
            break;
        case 5:
        {
            _signCtrl = [SingleLineInfoViewController new];
            _signCtrl.delegate = self;
            _signCtrl.singleType = 1;
            _signCtrl.placeHolderString = @"个性签名";
            _signCtrl.title = @"个性签名";
            [self.navigationController pushViewController:_signCtrl animated:YES];
        }
            break;
        case 6:
        {
            
        }
            break;
        default:
            break;
    }
    
    if (ctrl) {
        [self.navigationController pushViewController:ctrl animated:YES];
    }
    
}

#pragma mark - picker delegate
- (void)didDatePickedWithCtrl:(CustomDatePickerViewController *)ctrl date:(NSString *)date
{
    _infoData[3] = date == nil ? @"" : date;
    [self.tableView reloadData];
}

#pragma mark - area delegate

- (void)didAreaSelectedWithCtrl:(AreaTableViewController *)ctrl areaName:(NSString *)areaName
{
    _infoData[4] = areaName == nil ? @"" : areaName;
    
    [self.tableView reloadData];
}

#pragma mark - info delegate
- (void)didFillInfoDoneWithCtrl:(SingleLineInfoViewController *)ctrl info:(NSString *)info
{
    if (ctrl == _signCtrl) {
        _infoData[5] = info == nil ? @"" : info;
    }else{
        _infoData[1] = info == nil ? @"" : info;
    }
    [self.tableView reloadData];
}


-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{
    if (buttonIndex == 2) {
        return;
    }
    UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
    imagePicker.delegate = self;
    if (buttonIndex == 0) {
        imagePicker.sourceType  = UIImagePickerControllerSourceTypeCamera;
    }
    else if(buttonIndex == 1)
    {
        imagePicker.sourceType  = UIImagePickerControllerSourceTypePhotoLibrary;
    }
    
    [self presentViewController:imagePicker animated:YES completion:nil];
    

}

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
    UIImage *img = [info objectForKey:UIImagePickerControllerOriginalImage];
    [picker dismissViewControllerAnimated:YES completion:^{
        PECropViewController *peCropView = [[PECropViewController alloc] init];
        peCropView.delegate = self;
        peCropView.image = img;
        peCropView.toolbarHidden = YES;
        peCropView.keepingCropAspectRatio = YES;
        peCropView.imageCropRect = CGRectMake(0, 0, 200, 200);

        self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
        [self.navigationController pushViewController:peCropView animated:YES];
    }];
    
}


-(void)cropViewControllerDidCancel:(PECropViewController *)controller{
    [controller.navigationController popViewControllerAnimated:YES];
}

#pragma mark - 截取图片后上传
-(void)cropViewController:(PECropViewController *)controller didFinishCroppingImage:(UIImage *)croppedImage{
    [controller.navigationController popViewControllerAnimated:YES];
    [SVProgressHUD show];
    [[UserModel sharedModel] upLoadUserHeadImage:UIImageJPEGRepresentation(croppedImage, 0.5) result:^(BOOL success,NSString *path) {
        if (success) {
            [SVProgressHUD dismiss];
            NSLog(@"更新后URL地址:%@",[UserModel sharedModel].userInfo.headPic);
            [self _initVar];
            [[NSNotificationCenter defaultCenter] postNotificationName:@"UserHeadUpdate" object:nil];
        }else{
            [SVProgressHUD showErrorWithStatus:@"更新头像失败"];
        }
    }];
}




#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}

@end
